<template>
  <div class="supplier">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-collapse v-model="activeNames" @change="handleChange" accordion style="line-height: 48px;">
        <el-collapse-item title="供应商基础信息" name="1">
      <el-form-item label="供应商类型" prop="supplierType">
        <el-select v-model="dataForm.supplierType" placeholder="请选择">
          <el-option
            v-for="item in supplierTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="supplierCompanyName">
        <el-input v-model="dataForm.supplierCompanyName" placeholder="公司名称"></el-input>
      </el-form-item>
      <el-form-item label="公司介绍" prop="supplierCompanyDesc">
        <el-row :gutter="20">
          <el-col :span="20">
            <el-input v-model="dataForm.supplierCompanyDesc" placeholder="公司介绍"></el-input>
          </el-col>
          <el-col :span="1" class="login-captcha">
            <el-button type="primary" @click="uploadHandle()">上传文件</el-button>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="联系人" prop="supplierName">
        <el-input v-model="dataForm.supplierName" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="擅长业务类型" prop="supplierProjectType">
        <el-select v-model="dataForm.supplierProjectType" placeholder="请选择">
          <el-option
            v-for="item in supplierProjectTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配合时间段" prop="supplierProjectTime">
        <el-select v-model="dataForm.supplierProjectTime" placeholder="请选择">
          <el-option
            v-for="item in supplierProjectTimes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机" prop="supplierMobile">
        <el-input v-model="dataForm.supplierMobile" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="QQ" prop="supplierQq">
        <el-input v-model="dataForm.supplierQq" placeholder="QQ"></el-input>
      </el-form-item>
      <el-form-item label="微信" prop="supplierWeixin">
        <el-input v-model="dataForm.supplierWeixin" placeholder="微信"></el-input>
      </el-form-item>
        </el-collapse-item>
        <el-collapse-item title="合作方式" name="2">
      <el-form-item label="可接受的付款周期" prop="supplierJoinPeriod">
        <el-select v-model="dataForm.supplierJoinPeriod" placeholder="请选择">
          <el-option
            v-for="item in supplierJoinPeriods"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收费标准描述" prop="supplierJoinNeeds">
        <el-input v-model="dataForm.supplierJoinNeeds" placeholder="收费标准描述"></el-input>
      </el-form-item>
      <el-form-item label="支付宝账户" prop="supplierJoinZhifubaoAccount">
        <el-input v-model="dataForm.supplierJoinZhifubaoAccount" placeholder="支付宝账户"></el-input>
      </el-form-item>
      <el-form-item label="银行卡号" prop="supplierJoinBankcard">
        <el-input v-model="dataForm.supplierJoinBankcard" placeholder="银行卡号"></el-input>
      </el-form-item>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <div style="margin-top: 20px">
    <span slot="footer" class="dialog-footer" >
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </div>
    <!-- 弹窗, 上传文件 -->
    <upload v-if="uploadVisible" ref="upload"  @refreshDataList="updateNickName"></upload>
  </div>
</template>

<style>
  .supplier [class*=" el-icon-"], [class^=el-icon-] {
    line-height: 48px;
  }
  </style>
<script>
  import API from '@/api'
  import Upload from './upload'
  import store from '@/store'         // api: https://github.com/vuejs/vuex
  export default {
    data () {
      return {
        visible: false,
        uploadVisible: false,
        activeNames: ['1'],
        supplierTypes: [
          {
            value: 1,
            label: '个人'
          },
          {
            value: 0,
            label: '公司'
          }
        ],
        supplierProjectTimes: [
          {
            value: 1,
            label: '休息日'
          },
          {
            value: 2,
            label: '工作日'
          },
          {
            value: 3,
            label: '不限工作日和休息日'
          }
        ],
        supplierJoinPeriods: [
          {
            value: 1,
            label: '60天'
          },
          {
            value: 2,
            label: '45天'
          },
          {
            value: 3,
            label: '30天'
          },
          {
            value: 4,
            label: '其它'
          }
        ],
        supplierProjectTypes: [
          {
            value: 1,
            label: '撰稿'
          }, {
            value: 2,
            label: '方案'
          }, {
            value: 3,
            label: '设计海报'
          }, {
            value: 4,
            label: '设计漫画'
          }, {
            value: 5,
            label: '网站开发'
          }, {
            value: 6,
            label: 'h5开发'
          }, {
            value: 7,
            label: '视频拍摄制作'
          }, {
            value: 8,
            label: '线下活动'
          }, {
            value: 9,
            label: '媒介资源（新媒体）'
          }, {
            value: 10,
            label: '媒介资源（传统媒体）'
          }, {
            value: 11,
            label: '自定义'
          }
        ],
        dataForm: {
          id: 0,
          userId: '',
          supplierType: '',
          supplierCompanyName: '',
          supplierCompanyDesc: '',
          supplierName: '',
          supplierProjectType: '',
          supplierProjectTime: '',
          supplierMobile: '',
          supplierQq: '',
          supplierWeixin: '',
          supplierJoinPeriod: '',
          supplierJoinNeeds: '',
          supplierJoinZhifubaoAccount: '',
          supplierJoinBankcard: '',
          supplierAssetsHow: '',
          supplierAssetsXg: '',
          supplierAssetsXl: '',
          supplierAssetsTd: '',
          supplierAssetsCs: '',
          supplierAssetsSf: '',
          supplierAssetsYs: '',
          supplierAssetsCount: '',
          supplierRemark: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          supplierType: [
            { required: true, message: '供应商类型不能为空', trigger: 'blur' }
          ],
          supplierCompanyName: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' }
          ],
          supplierCompanyDesc: [
            { required: true, message: '公司介绍不能为空', trigger: 'blur' }
          ],
          supplierName: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          supplierProjectType: [
            { required: true, message: '擅长业务类型不能为空', trigger: 'blur' }
          ],
          supplierProjectTime: [
            { required: true, message: '配合时间段不能为空', trigger: 'blur' }
          ],
          supplierMobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          supplierJoinPeriod: [
            { required: true, message: '可接受的付款周期不能为空', trigger: 'blur' }
          ],
          supplierJoinNeeds: [
            { required: true, message: '收费标准描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      Upload
    },
    activated () {
      this.init(0)
    },
    methods: {
      init (id) {
        this.dataForm.userId = store.state.user.id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            API.basesupplier.infoByUserId(this.dataForm.userId).then(({data}) => {
              console.log(data)
              if (data && data.code === 0 && data.basesupplier) {
                this.dataForm.id = data.basesupplier.id
                this.dataForm.userId = data.basesupplier.userId
                this.dataForm.supplierType = data.basesupplier.supplierType
                this.dataForm.supplierCompanyName = data.basesupplier.supplierCompanyName
                this.dataForm.supplierCompanyDesc = data.basesupplier.supplierCompanyDesc
                this.dataForm.supplierName = data.basesupplier.supplierName
                this.dataForm.supplierProjectType = data.basesupplier.supplierProjectType
                this.dataForm.supplierProjectTime = data.basesupplier.supplierProjectTime
                this.dataForm.supplierMobile = data.basesupplier.supplierMobile
                this.dataForm.supplierQq = data.basesupplier.supplierQq
                this.dataForm.supplierWeixin = data.basesupplier.supplierWeixin
                this.dataForm.supplierJoinPeriod = data.basesupplier.supplierJoinPeriod
                this.dataForm.supplierJoinNeeds = data.basesupplier.supplierJoinNeeds
                this.dataForm.supplierJoinZhifubaoAccount = data.basesupplier.supplierJoinZhifubaoAccount
                this.dataForm.supplierJoinBankcard = data.basesupplier.supplierJoinBankcard
                this.dataForm.supplierAssetsHow = data.basesupplier.supplierAssetsHow
                this.dataForm.supplierAssetsXg = data.basesupplier.supplierAssetsXg
                this.dataForm.supplierAssetsXl = data.basesupplier.supplierAssetsXl
                this.dataForm.supplierAssetsTd = data.basesupplier.supplierAssetsTd
                this.dataForm.supplierAssetsCs = data.basesupplier.supplierAssetsCs
                this.dataForm.supplierAssetsSf = data.basesupplier.supplierAssetsSf
                this.dataForm.supplierAssetsYs = data.basesupplier.supplierAssetsYs
                this.dataForm.supplierAssetsCount = data.basesupplier.supplierAssetsCount
                this.dataForm.supplierRemark = data.basesupplier.supplierRemark
                this.dataForm.createUserId = data.basesupplier.createUserId
                this.dataForm.createTime = data.basesupplier.createTime
              }
            })
          }
        })
      },
      // 上传文件
      uploadHandle () {
        this.uploadVisible = true
        this.$nextTick(() => {
          this.$refs.upload.init()
        })
      },
      updateNickName (supplierCompanyDesc) {
        this.dataForm.supplierCompanyDesc = supplierCompanyDesc
      },
      handleChange (val) {
        console.log(val)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'userId': store.state.user.id,
              'supplierType': this.dataForm.supplierType,
              'supplierCompanyName': this.dataForm.supplierCompanyName,
              'supplierCompanyDesc': this.dataForm.supplierCompanyDesc,
              'supplierName': this.dataForm.supplierName,
              'supplierProjectType': this.dataForm.supplierProjectType,
              'supplierProjectTime': this.dataForm.supplierProjectTime,
              'supplierMobile': this.dataForm.supplierMobile,
              'supplierQq': this.dataForm.supplierQq,
              'supplierWeixin': this.dataForm.supplierWeixin,
              'supplierJoinPeriod': this.dataForm.supplierJoinPeriod,
              'supplierJoinNeeds': this.dataForm.supplierJoinNeeds,
              'supplierJoinZhifubaoAccount': this.dataForm.supplierJoinZhifubaoAccount,
              'supplierJoinBankcard': this.dataForm.supplierJoinBankcard,
              'supplierAssetsHow': this.dataForm.supplierAssetsHow,
              'supplierAssetsXg': this.dataForm.supplierAssetsXg,
              'supplierAssetsXl': this.dataForm.supplierAssetsXl,
              'supplierAssetsTd': this.dataForm.supplierAssetsTd,
              'supplierAssetsCs': this.dataForm.supplierAssetsCs,
              'supplierAssetsSf': this.dataForm.supplierAssetsSf,
              'supplierAssetsYs': this.dataForm.supplierAssetsYs,
              'supplierAssetsCount': this.dataForm.supplierAssetsCount,
              'supplierRemark': this.dataForm.supplierRemark,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime
            }
            var tick = !this.dataForm.id ? API.basesupplier.add(params) : API.basesupplier.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
